//
//  MSMenuView.m
//  厚积薄发
//
//  Created by 侯振兴 on 14/12/31.
//  Copyright (c) 2014年 侯振兴. All rights reserved.
//



#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MSMenuView.h"

#define kBlueColor [UIColor colorWithRed:129/255.0 green: 198/255.0 blue: 221/255.0 alpha: 1.0]


@implementation MSMenuView
@synthesize  PlaylistBtn,ArtistBtn,AlbumBtn,AllSongsBtn,MoreBtn,delegate;


- (id)initWithFrame:(CGRect)frame
{
    CGRect frame1 = CGRectMake(frame.origin.x, frame.origin.y, SCREEN_WIDTH, 165);
    
    self = [super initWithFrame:frame1];
    if (self) {
        
        [self setBackgroundColor:[UIColor whiteColor]];
        
        PlaylistBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [PlaylistBtn setFrame:CGRectMake(1, 7, self.frame.size.width / 5 - 1, 71)];
        [PlaylistBtn setSelected:YES];
        [PlaylistBtn setTag:1];
        [PlaylistBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [PlaylistBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
        PlaylistBtn.contentEdgeInsets = UIEdgeInsetsMake(4, 0, 0, 0);
        [PlaylistBtn setBackgroundColor:kBlueColor];
        [PlaylistBtn setImage:[UIImage imageNamed:@"yuelanshi.png"] forState:UIControlStateNormal];
        [PlaylistBtn setTitle:@"阅" forState:UIControlStateNormal];
        [PlaylistBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:PlaylistBtn];
        
        
        ArtistBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ArtistBtn setFrame:CGRectMake(self.frame.size.width / 5 + 1, 27, self.frame.size.width / 5 - 1, 71)];
        [ArtistBtn setTag:2];
        [ArtistBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [ArtistBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
        ArtistBtn.contentEdgeInsets = UIEdgeInsetsMake(4, 0, 0, 0);
        [ArtistBtn setBackgroundColor:kBlueColor];
        [ArtistBtn setImage:[UIImage imageNamed:@"sousuo.png"] forState:UIControlStateNormal];
        [ArtistBtn setTitle:@"搜" forState:UIControlStateNormal];
        [ArtistBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:ArtistBtn];

        
        AlbumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [AlbumBtn setFrame:CGRectMake(2 * self.frame.size.width / 5 + 1, 27, self.frame.size.width / 5 - 1, 71)];
        [AlbumBtn setTag:3];
        [AlbumBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [AlbumBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
        AlbumBtn.contentEdgeInsets = UIEdgeInsetsMake(4, 0, 0, 0);
        [AlbumBtn setImage:[UIImage imageNamed:@"zhuye.png"] forState:UIControlStateNormal];
        [AlbumBtn setBackgroundColor:kBlueColor];
        [AlbumBtn setTitle:@"首" forState:UIControlStateNormal];
        [AlbumBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:AlbumBtn];

        AllSongsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [AllSongsBtn setFrame:CGRectMake(3 * self.frame.size.width / 5 + 1, 27, self.frame.size.width / 5 - 2, 71)];
        [AllSongsBtn setTag:4];
        [AllSongsBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [AllSongsBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
        AllSongsBtn.contentEdgeInsets = UIEdgeInsetsMake(4, 0, 0, 0);
//        [AllSongsBtn setBackgroundImage:[UIImage imageNamed:@"TabBarBG.png"] forState:UIControlStateNormal];
//        [AllSongsBtn setBackgroundImage:[UIImage imageNamed:@"TabBarBG.png"] forState:UIControlStateSelected];
        [AllSongsBtn setImage:[UIImage imageNamed:@"shoucang.png"] forState:UIControlStateNormal];
        [AllSongsBtn setBackgroundColor:kBlueColor];
        [AllSongsBtn setTitle:@"藏" forState:UIControlStateNormal];
        [AllSongsBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:AllSongsBtn];

        
        MoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [MoreBtn setFrame:CGRectMake(4 * self.frame.size.width / 5, 27, self.frame.size.width / 5 - 2, 71)];
        [MoreBtn setTag:5];
        [MoreBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [MoreBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
         MoreBtn.contentEdgeInsets = UIEdgeInsetsMake(4, 0, 0, 0);
        [MoreBtn setBackgroundColor:kBlueColor];
        [MoreBtn setImage:[UIImage imageNamed:@"shezhi.png"] forState:UIControlStateNormal];
        [MoreBtn setTitle:@"置" forState:UIControlStateNormal];
        [MoreBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:MoreBtn];
        
        
        
        
    }
    
  
    return self;
    
    
}

//Need to add tag
-(void)checkSelectedBtn:(UIButton *)sender{
    
    if (PlaylistBtn.selected && PlaylistBtn.tag != sender.tag) {
        CGRect TempFrame = PlaylistBtn.frame;
        [UIView animateWithDuration:0.3 animations:^{
        [PlaylistBtn setFrame:CGRectMake(TempFrame.origin.x, TempFrame.origin.y+20, TempFrame.size.width, TempFrame.size.height)];
         }completion:^(BOOL finished) {
            
        }];
        PlaylistBtn.selected = NO;
            
        
    }
    else if (ArtistBtn.selected && ArtistBtn.tag != sender.tag) {
        CGRect TempFrame = ArtistBtn.frame;
         [UIView animateWithDuration:0.3 animations:^{
        [ArtistBtn setFrame:CGRectMake(TempFrame.origin.x, TempFrame.origin.y+20, TempFrame.size.width, TempFrame.size.height)];
         }completion:^(BOOL finished) {
             
         }];
        ArtistBtn.selected = NO;
    }
   else if (AlbumBtn.selected && AlbumBtn.tag!= sender.tag) {
       CGRect TempFrame = AlbumBtn.frame;
       [UIView animateWithDuration:0.3 animations:^{
       [AlbumBtn setFrame:CGRectMake(TempFrame.origin.x, TempFrame.origin.y+20, TempFrame.size.width, TempFrame.size.height)];
       }completion:^(BOOL finished) {
           
       }];
       AlbumBtn.selected = NO;
    }
   else if (AllSongsBtn.selected && AllSongsBtn.tag != sender.tag) {
       CGRect TempFrame = AllSongsBtn.frame;
       [UIView animateWithDuration:0.3 animations:^{
       [AllSongsBtn setFrame:CGRectMake(TempFrame.origin.x, TempFrame.origin.y+20, TempFrame.size.width, TempFrame.size.height)];
       }completion:^(BOOL finished) {
           
       }];
       AllSongsBtn.selected = NO;
   }
   else if (MoreBtn.selected && MoreBtn.tag != sender.tag) {
       CGRect TempFrame = MoreBtn.frame;
        [UIView animateWithDuration:0.3 animations:^{
       [MoreBtn setFrame:CGRectMake(TempFrame.origin.x, TempFrame.origin.y+20, TempFrame.size.width, TempFrame.size.height)];
        }completion:^(BOOL finished) {
            
        }];
       MoreBtn.selected = NO;
   }

}

-(void)callButtonAction:(UIButton *)sender
    {
    if (sender.tag == 1) {
        [self.delegate PlaylistBtnClick];
    }
    if (sender.tag == 2) {
        [self.delegate ArtistBtnClick];
      }
    if (sender.tag == 3) {
        [self.delegate AlbumBtnClick];
    }
    if (sender.tag == 4) {
        [self.delegate AllSongsBtnClick];
    }
    if (sender.tag == 5) {
        [self.delegate MoreBtnClick];
     }
}

-(void)buttonClickAction:(id)sender{
        UIButton *btn = (UIButton *)sender;
        CGRect rec = btn.frame;
        if (!btn.selected) {
           [UIView animateWithDuration:0.3 animations:^{
                [btn setFrame:CGRectMake(rec.origin.x, rec.origin.y-20, rec.size.width, rec.size.height)];
           } completion:^(BOOL finished) {
               
    
            }];
            btn.selected = YES;
            [self checkSelectedBtn:btn];
            [self callButtonAction:btn];
       }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
